package api

import (
	"github.com/gin-gonic/gin"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	"go-caipu/pkg/services/admin/post"
)

func (hs *HTTPServer) GetPostPage(c *gin.Context) {
	cmd := post.GetPostPageCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, count, err := hs.postService.GetPostPage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}
func (hs *HTTPServer) GetPost(c *gin.Context) {
	cmd := post.GetPostCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	result, err := hs.postService.Get(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithData(c, result)

}
func (hs *HTTPServer) CreatePost(c *gin.Context) {
	cmd := post.Post{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.postService.Insert(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdatePost(c *gin.Context) {
	cmd := post.Post{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.postService.Update(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) RemovePost(c *gin.Context) {
	cmd := post.RemovePostCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.postService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
